package push.tools.service.framework;

import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import push.tools.service.framework.service.BaseHandleService;
import push.tools.service.framework.service.SecurityRequestService;

import java.util.HashMap;

/**
 * 处理请求的基类
 */
public abstract class PostController extends CryptoSupport {
    private static final Logger logger = LoggerFactory.getLogger(PostController.class);

    //子类具体实现
    protected abstract String postRequest(HashMap<String, String> requestParam);

    @Autowired
    public BaseHandleService baseHandleService; //处理基础请求

    @Autowired
    public SecurityRequestService securityRequestService;//接口安全校验

    protected String handleRequest(HttpServletRequest req) {
        // 获取基本请求信息
        HashMap<String, String> requestDataMap = baseHandleService.getRequestData(req);
        if (!requestDataMap.get("state").equals("000")) {
            return generateFail(requestDataMap.get("msg"));
        }
        // 安全前置校验
        String requestBody = requestDataMap.get("requestBody");
        HashMap<String, String> preVerificationMap = securityRequestService.preVerification(requestBody);
        if (!preVerificationMap.get("state").equals("000")) {
            return generateFail(preVerificationMap.get("msg"));
        }
        return postRequest(requestDataMap);
    }

}
